package com.yh.tea.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yh.tea.entity.Goods;
import com.yh.tea.model.GoodsQuery;
import com.yh.tea.service.GoodsService;
import com.yh.tea.utiity.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yh
 * @since 2024-03-18
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    /***
     * 商品name
     * 商品主图
     * 商品详情页
     */
    @Autowired
    private GoodsService goodsService;
    //添加商品
    @PostMapping("/add")
    public ResponseResult add(@RequestBody Goods goods) {
        Boolean result =goodsService.save(goods);
        return ResponseResult.success(result?"添加成功":"添加失败",result);
    }
    //修改
    @PostMapping("/update")
    public ResponseResult update(@RequestBody Goods goods){
        Boolean result=goodsService.updateById(goods);
        return ResponseResult.success(result?"修改成功":"修改失败",result);
    }
    //删除
    @PostMapping("/del")
    public ResponseResult del(@RequestBody List<Long> goodsIds){
        for (Long goodsId : goodsIds) {
            goodsService.remove(new QueryWrapper<Goods>().eq("id",goodsId));
        }
        return ResponseResult.success("删除成功");
    }
    //分页查询
    @PostMapping("/getPage")
    public ResponseResult getPage(@RequestBody GoodsQuery goodsQuery){
        return goodsService.getPage(goodsQuery);
    }

}

